//
//  ShiWuFooter.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/21.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "ShiWuFooter.h"
#import "EnsureAddressView.h"
#import "ShowPrizeVC.h"
#import "WYPublicWeb.h"
//确认收货
#define ENSURE_URL @"/api/ApiUser/confirmReceivedGoods"
//快递查询
@interface ShiWuFooter ()<UIAlertViewDelegate>
{
    __weak IBOutlet UILabel *nameLab;
    __weak IBOutlet UILabel *codeLab;
    
    __weak IBOutlet UIView *bgview;
    //状态lab
    __weak IBOutlet UILabel *slab1;
    __weak IBOutlet UILabel *slab2;
    __weak IBOutlet UILabel *slab3;
    __weak IBOutlet UILabel *slab4;
    __weak IBOutlet UILabel *slab5;
    //状态img
    __weak IBOutlet UIImageView *img1;
    __weak IBOutlet UIImageView *img2;
    __weak IBOutlet UIImageView *img3;
    __weak IBOutlet UIImageView *img4;
    __weak IBOutlet UIImageView *img5;
    
    __weak IBOutlet UIImageView *line1;
    __weak IBOutlet UIImageView *line2;
    __weak IBOutlet UIImageView *line3;
    __weak IBOutlet UIImageView *line4;
    __weak IBOutlet UIImageView *line5;
    __weak IBOutlet UIImageView *line6;
    __weak IBOutlet UIImageView *line7;
    __weak IBOutlet UIImageView *line8;
    
    __weak IBOutlet UILabel *timeLab1;
    __weak IBOutlet UILabel *timeLab2;
    __weak IBOutlet UILabel *timeLab3;
    __weak IBOutlet UILabel *timeLab4;
    __weak IBOutlet UILabel *timeLab5;
    
    __weak IBOutlet UIButton *ensureAddressBtn;
    __weak IBOutlet UIButton *ensureBtn;
    __weak IBOutlet UIButton *waitingBtn;
    
    __weak IBOutlet UIButton *showBtn;
    __weak IBOutlet UILabel *o_nameLab;
    __weak IBOutlet UILabel *o_add_lab;
    __weak IBOutlet UILabel *o_phoneLab;
    
    //隐藏的
    
    __weak IBOutlet UILabel *xc1;
    __weak IBOutlet UILabel *xc2;
    __weak IBOutlet UILabel *xc3;
    __weak IBOutlet UILabel *xc4;
    
    //打码按钮
    __weak IBOutlet UIButton *damaBtn;
    
    __weak IBOutlet UILabel *tishiLabel;
    
    
    UIView *fenge1;
    UIView *fenge2;
    UIView *fenge3;
    UIView *fenge4;
    
    EnsureAddressView *getv;
    //地址信息左边的白背景
    UIView *whitebgfg;
    
    //是否打码标志
    BOOL isDamaflag;
    //记录原始显示值
    NSString *_ysname;
    NSString *_ysphone;
    NSString *_ysadd;
    NSString *_codeNum;
}
@end
@implementation ShiWuFooter

- (void)awakeFromNib
{
    
    CGFloat add = 4;
    self.backgroundColor = QBB_GRAY_COLOR;
    //打码按钮
    [damaBtn setImage:[UIImage imageNamed:@"icon_eye_press"] forState:UIControlStateSelected];
    [bgview addSubview:[WYPublic getFenGexian:0 :0 :WYMWITH :0.5]];
    [bgview addSubview:[WYPublic getFenGexian:CGRectGetMinX(slab1.frame) :CGRectGetMaxY(slab1.frame)+add :WYMWITH  :0.5]];
    [bgview addSubview:[WYPublic getFenGexian:CGRectGetMinX(slab2.frame) :CGRectGetMaxY(slab2.frame)+add :WYMWITH  :0.5]];
    [bgview addSubview:[WYPublic getFenGexian:CGRectGetMinX(slab3.frame) :CGRectGetMaxY(slab3.frame)+add :WYMWITH  :0.5]];
    [bgview addSubview:[WYPublic getFenGexian:CGRectGetMinX(slab4.frame) :CGRectGetMaxY(slab4.frame)+add :WYMWITH  :0.5]];
    [bgview addSubview:[WYPublic getFenGexian:0 :CGRectGetMaxY(slab5.frame)+add+1 :WYMWITH  :0.5]];
    
    ensureAddressBtn.hidden = YES;
    ensureBtn.hidden = YES;
    showBtn.hidden = YES;
    waitingBtn.hidden = YES;
    
    
    ensureAddressBtn.layer.cornerRadius = 2;
    ensureAddressBtn.layer.masksToBounds = YES;
    ensureAddressBtn.backgroundColor = QBB_RED_COLOR;

    ensureBtn.layer.cornerRadius = 2;
    ensureBtn.layer.masksToBounds = YES;
    ensureBtn.backgroundColor = QBB_RED_COLOR;
    showBtn.layer.cornerRadius = 2;
    showBtn.layer.masksToBounds = YES;
    showBtn.backgroundColor = QBB_RED_COLOR;
    
    waitingBtn.layer.cornerRadius = 2;
    waitingBtn.layer.masksToBounds = YES;
    [waitingBtn setTitleColor:QBB_RED_COLOR forState:UIControlStateNormal];
    
    fenge1 = [WYPublic getFenGexian:0 :CGRectGetMaxY(xc1.frame) :WYMWITH :0.5];
    fenge2 = [WYPublic getFenGexian:0 :CGRectGetMinY(xc2.frame) :WYMWITH :0.5];
    fenge3 = [WYPublic getFenGexian:0 :CGRectGetMaxY(xc2.frame) :WYMWITH :0.5];
    fenge4 = [WYPublic getFenGexian:0 :CGRectGetMaxY(tishiLabel.frame) :WYMWITH :0.5];
    whitebgfg = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(xc1.frame), 8, CGRectGetMinY(xc2.frame)-CGRectGetMaxY(xc1.frame))];
    whitebgfg.backgroundColor = [UIColor whiteColor];
    [self addSubview:whitebgfg];
    [self addSubview:fenge1];
    [self addSubview:fenge2];
    [self addSubview:fenge3];
    [self addSubview:fenge4];
    
    tishiLabel.text = @"提示 :部分商品可能是京东第三方买家发货,若出现京东订单无法查询的问题,请耐心等待几天.若需帮助,请联系夺宝吧客服(0755-86250743)";
    tishiLabel.textColor = QBB_RED_COLOR;
    tishiLabel.backgroundColor = [UIColor whiteColor];
    UIView *whiteLeftView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(xc4.frame), 8, CGRectGetHeight(tishiLabel.frame))];
    whiteLeftView.backgroundColor = [UIColor whiteColor];
    UIView *whiteRightView = [[UIView alloc]initWithFrame:CGRectMake( WYMWITH - 8, CGRectGetMaxY(xc4.frame), 8, CGRectGetHeight(tishiLabel.frame))];
    whiteRightView.backgroundColor = [UIColor whiteColor];
    [self addSubview:whiteLeftView];
    [self addSubview:whiteRightView];
    
}
- (void)setIsMineFlag:(BOOL)isMineFlag
{
    _isMineFlag = isMineFlag;
    if (!_isMineFlag) {
        [self hiddenWuLiu];
        [self hiddenAddress];
    }
}
- (void)setModel:(RewardModel *)model
{
    _model = model;
    if ([model.reward_owner_name class] != [NSNull class]) {
        o_nameLab.text = [NSString stringWithFormat:@"收货人姓名:%@",model.reward_owner_name];
    }
    o_add_lab.text = [NSString stringWithFormat:@"收货地址:%@",model.reward_address];
    if ([model.reward_phone class] != [NSNull class]) {
        o_phoneLab.text = [NSString stringWithFormat:@"联系电话:%@",model.reward_phone];
    }
    if ([_model.reward_company class] != [NSNull class]) {
        nameLab.text = _model.reward_company;
    }else{
        nameLab.text = @"物流公司不详";
    }
    if ([_model.reward_company_code class] != [NSNull class]) {
        codeLab.text = _model.reward_company_code;
    }else{
        codeLab.text = @"物流单号不详";
        codeLab.textColor = [UIColor darkGrayColor];
    }
    //设置订单状态值
    if ([model.reward_current_status_value class] != [NSNull class]) {
        switch ([model.reward_current_status_value integerValue]) {
            case 201:/**************获得奖品,就要确认地址了*****************/
            {
                img2.image = [UIImage imageNamed:@"icon_circle_red"];
                img3.image = [UIImage imageNamed:@"icon_circle_span"];
                img4.image = [UIImage imageNamed:@"icon_circle_span"];
                img5.image = [UIImage imageNamed:@"icon_circle_span"];
                slab3.textColor = [UIColor lightGrayColor];
                slab4.textColor = [UIColor lightGrayColor];
                slab5.textColor = [UIColor lightGrayColor];
                
                line2.image = nil;
                line2.backgroundColor = QBB_RED_COLOR;
                line3.image = [UIImage imageNamed:@"line_2"];
                line4.image = [UIImage imageNamed:@"line_2"];
                line5.image = [UIImage imageNamed:@"line_2"];
                line6.image = [UIImage imageNamed:@"line_2"];
                line7.image = [UIImage imageNamed:@"line_2"];
                line8.image = [UIImage imageNamed:@"line_2"];
                
                timeLab2.hidden = YES;
                timeLab3.hidden = YES;
                timeLab4.hidden = YES;
                timeLab5.hidden = YES;
                if (_isMineFlag) {
                    ensureAddressBtn.hidden = NO;
                }else{
                    UILabel *goLab = [[UILabel alloc]initWithFrame:ensureAddressBtn.frame];
                    goLab.textAlignment = NSTextAlignmentCenter;
                    goLab.font = [UIFont systemFontOfSize:14];
                    goLab.text = @"进行中…";
                    goLab.textColor = QBB_RED_COLOR;
                    [bgview addSubview:goLab];
                }
                [self hiddenAddress];
                [self hiddenWuLiu];
                //取时间
                if ([model.reward_status_list class] != [NSNull class] && model.reward_status_list.count > 0) {
                    timeLab1.text = model.reward_status_list[0][@"time"];
                }
            }
                break;
            case 202:/**************确认收货地址,等待派发奖品*****************/
            {
                img3.image = [UIImage imageNamed:@"icon_circle_red"];
                img4.image = [UIImage imageNamed:@"icon_circle_span"];
                img5.image = [UIImage imageNamed:@"icon_circle_span"];
                slab4.textColor = [UIColor lightGrayColor];
                slab5.textColor = [UIColor lightGrayColor];
                
                line4.image = nil;
                line4.backgroundColor = QBB_RED_COLOR;
                line5.image = [UIImage imageNamed:@"line_2"];
                line6.image = [UIImage imageNamed:@"line_2"];
                line7.image = [UIImage imageNamed:@"line_2"];
                line8.image = [UIImage imageNamed:@"line_2"];
                
                timeLab3.hidden = YES;
                waitingBtn.hidden = NO;
                whitebgfg.hidden = NO;
                
                timeLab4.hidden = YES;
                timeLab5.hidden = YES;
                [self hiddenWuLiu];
                
                //取时间
                if ([model.reward_status_list class] != [NSNull class] && model.reward_status_list.count > 1) {
                    timeLab1.text = model.reward_status_list[0][@"time"];
                    timeLab2.text = model.reward_status_list[1][@"time"];
                }
            }
                break;
            case 203:/**************奖品派发*****************/
            {
                img4.image = [UIImage imageNamed:@"icon_circle_red"];
                img5.image = [UIImage imageNamed:@"icon_circle_span"];
                slab5.textColor = [UIColor lightGrayColor];
                
                line6.image = nil;
                line6.backgroundColor = QBB_RED_COLOR;
                line7.image = [UIImage imageNamed:@"line_2"];
                line8.image = [UIImage imageNamed:@"line_2"];
                
                timeLab4.hidden = YES;
                timeLab5.hidden = YES;
                
                if (_isMineFlag) {
                    ensureBtn.hidden = NO;
                }else{
                    UILabel *goLab = [[UILabel alloc]initWithFrame:ensureBtn.frame];
                    goLab.textAlignment = NSTextAlignmentCenter;
                    goLab.font = [UIFont systemFontOfSize:14];
                    goLab.text = @"进行中…";
                    goLab.textColor = QBB_RED_COLOR;
                    [bgview addSubview:goLab];

                }
                
                
                //取时间
                if ([model.reward_status_list class] != [NSNull class] && model.reward_status_list.count > 2) {
                    timeLab1.text = model.reward_status_list[0][@"time"];
                    timeLab2.text = model.reward_status_list[1][@"time"];
                    timeLab3.text = model.reward_status_list[2][@"time"];
                }

            }
                break;
            case 204:/**************确认收货*****************/
            {
                img5.image = [UIImage imageNamed:@"icon_circle_red"];
                
                line8.image = nil;
                line8.backgroundColor = QBB_RED_COLOR;
                timeLab5.hidden = YES;
                
                if (_isMineFlag) {
                    showBtn.hidden = NO;
                }else{
                    UILabel *goLab = [[UILabel alloc]initWithFrame:showBtn.frame];
                    goLab.textAlignment = NSTextAlignmentCenter;
                    goLab.font = [UIFont systemFontOfSize:14];
                    goLab.text = @"进行中…";
                    goLab.textColor = QBB_RED_COLOR;
                    [bgview addSubview:goLab];
                }
                
                
                //取时间
                if ([model.reward_status_list class] != [NSNull class] && model.reward_status_list.count > 3) {
                    timeLab1.text = model.reward_status_list[0][@"time"];
                    timeLab2.text = model.reward_status_list[1][@"time"];
                    timeLab3.text = model.reward_status_list[2][@"time"];
                    timeLab4.text = model.reward_status_list[3][@"time"];
                }

            }
                break;
            case 205:/**************晒单分享*****************/
            {
                img5.image = [UIImage imageNamed:@"icon_circle_red"];
                
                //取时间
                if ([model.reward_status_list class] != [NSNull class] && model.reward_status_list.count > 4) {
                    timeLab1.text = model.reward_status_list[0][@"time"];
                    timeLab2.text = model.reward_status_list[1][@"time"];
                    timeLab3.text = model.reward_status_list[2][@"time"];
                    timeLab4.text = model.reward_status_list[3][@"time"];
                    timeLab5.text = model.reward_status_list[4][@"time"];
                }

            }
                break;
            default:
                break;
        }
    }
}
//隐藏地址信息
- (void)hiddenAddress
{
    xc1.hidden = YES;
    damaBtn.hidden = YES;
    damaBtn.hidden = YES;
    o_phoneLab.hidden = YES;
    o_add_lab.hidden = YES;
    o_nameLab.hidden = YES;
    fenge1.hidden = YES;
    fenge2.hidden = YES;
    whitebgfg.hidden = YES;
}
//隐藏物流信息
- (void)hiddenWuLiu
{
    xc2.hidden = YES;
    xc3.hidden = YES;
    xc4.hidden = YES;
    nameLab.hidden = YES;
    codeLab.hidden = YES;
    fenge3.hidden = YES;
    fenge4.hidden = YES;
    
}
//确认收货地址
- (IBAction)ensureAddressOnClick:(id)sender {
    [self addZheZhao];
    getv = [[[NSBundle mainBundle] loadNibNamed:@"EnsureAddressView" owner:self options:nil]lastObject];
    //    [getv morenzhi:[nameBtn titleForState:UIControlStateNormal]];
    getv.father =self;
    getv.order_id = _model.reward_order_id;
    [self.father.view addSubview:getv];
    getv.frame = CGRectMake(WYMWITH/2 - getv.frame.size.width/2, -1*getv.frame.size.height, getv.frame.size.width, getv.frame.size.height);
    [UIView animateWithDuration:0.15 animations:^{
        getv.center = CGPointMake(WYMWITH/2, WYMHEIGHT/2-100*AUTOY);
    }];

}

//确定收货
- (IBAction)ensureBtnOnClick:(id)sender {
    UIAlertView *alt = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确认您已收到奖品？" delegate:self cancelButtonTitle:@"还未收到" otherButtonTitles:@"已收到", nil];
    [alt show];
}

//我要晒单
- (IBAction)showBtnOnclick:(id)sender {
    ShowPrizeVC *showVC = [[ShowPrizeVC alloc]init];
    showVC.model = self.model;
    [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:showVC animated:YES];
}
//添加一个遮罩层
- (void)addZheZhao
{
    UIView *zheZhao = [[UIView alloc]initWithFrame:CGRectMake(0,0,WYMWITH, WYMHEIGHT)];
    zheZhao.tag = 1111;
    zheZhao.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgOnClick)];
    [zheZhao addGestureRecognizer:tap];
    [self.father.view addSubview:zheZhao];
}
- (void)bgOnClick
{
    [self removeZheZhao];
    [getv removeFromSuperview];
}
//去掉遮罩
- (void)removeZheZhao
{
    UIView *zhezhao = [self.father.view viewWithTag:1111];
    [zhezhao removeFromSuperview];
}
//填充默认地址
- (void)fillAddressWith:(NSString *)name Phone:(NSString *)phone Add:(NSString *)add Andtime:(NSString *)time
{
    if ([name class] != [NSNull class] && name.length != 0) {
        if (_isMineFlag) {
            xc1.hidden = NO;
            damaBtn.hidden = NO;
            o_phoneLab.hidden = NO;
            o_add_lab.hidden = NO;
            o_nameLab.hidden = NO;
            fenge1.hidden = NO;
            fenge2.hidden = NO;
            whitebgfg.hidden = NO;
        }
        o_nameLab.text = [NSString stringWithFormat:@"收货人姓名:%@",[WYPublic getUsefullStr:name]];
        if ([name class] != [NSNull class]) {
            o_add_lab.text = [NSString stringWithFormat:@"收货地址:%@",add];
        }
        if ([add class] != [NSNull class]) {
            o_phoneLab.text = [NSString stringWithFormat:@"联系电话:%@",phone];
        }

        timeLab2.hidden = NO;
        if ([time class] != [NSNull class]) {
            timeLab2.text = time;
        }
        ensureAddressBtn.hidden = YES;
        timeLab3.hidden = YES;
        waitingBtn.hidden = NO;
        
        img2.image = [UIImage imageNamed:@"icon_circle_gray"];
        img3.image = [UIImage imageNamed:@"icon_circle_red"];
        line2.backgroundColor = [UIColor clearColor];
        line2.image = [UIImage imageNamed:@"line_1"];
        line3.image = [UIImage imageNamed:@"line_1"];
        line4.image = nil;
        line4.backgroundColor = QBB_RED_COLOR;
        
        slab3.textColor = [UIColor blackColor];
        
        //改变状态
        _model.reward_current_status_value = [NSNumber numberWithInteger:202];
        _model.reward_current_status = @"确认收货地址";
    }
}

//确定收到奖品对话框
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1){
        [WYPublic addZheZhaoToWindowWithTitle:nil];
        //确认收货
        NSDictionary *params = @{@"record_id":self.model.reward_order_id,@"uid":[UserInfo getUserID],@"token":[UserInfo getUserToken],@"refresh_token":[UserInfo getRefreshToken]};
        [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,ENSURE_URL] Parameters:params successBlock:^(NSDictionary *dic) {
            NSNumber *ret = dic[@"ret"];
            if ([ret integerValue] == 0) {
                if ([dic[@"data"] class] != [NSNull class]) {
                    timeLab4.hidden = NO;
                    ensureBtn.hidden = YES;
                    timeLab4.text = dic[@"data"];
                    img4.image = [UIImage imageNamed:@"icon_circle_gray"];
                    img5.image = [UIImage imageNamed:@"icon_circle_red"];
                    line6.backgroundColor = [UIColor clearColor];
                    line6.image = [UIImage imageNamed:@"line_1"];
                    line7.image = [UIImage imageNamed:@"line_1"];
                    line8.image = nil;
                    line8.backgroundColor = QBB_RED_COLOR;
                    slab5.textColor = [UIColor blackColor];
                    if (_isMineFlag) {
                        showBtn.hidden = NO;
                    }
                    //改变状态
                    _model.reward_current_status_value = [NSNumber numberWithInteger:204];
                    _model.reward_current_status = @"确认收货";
                }
            }
            [WYPublic removeZheZhaoToWindow];
        } failedBlock:^{
            [WYPublic removeZheZhaoToWindow];
        } needLog:NO];
    }
}

//快递查询
- (IBAction)kuaidi:(id)sender {
    if ([_model.reward_company_code class] == [NSNull class]) {
        return;
    }
    if ([_model.reward_company_mark class] == [NSNull class]) {
        return;
    }
    WYPublicWeb *web = [[WYPublicWeb alloc]init];
    web.mtitle = @"快递查询";
    web.urlStr = [NSString stringWithFormat:@"http://m.kuaidi100.com/index_all.html?type=%@&postid=%@",_model.reward_company_mark,_model.reward_company_code];
    [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:web animated:YES];
}
//打码
- (IBAction)damaBtnOnClick:(id)sender {
    
    if (isDamaflag) {
        o_nameLab.text = _ysname;
        o_phoneLab.text = _ysphone;
        o_add_lab.text = _ysadd;
        codeLab.text = _codeNum
        ;
    }else{
        _ysadd = o_add_lab.text;
        _ysname = o_nameLab.text;
        _ysphone = o_phoneLab.text;
        _codeNum  = codeLab.text;
        
        if ((o_nameLab.text.length - 2)>5) {
            o_nameLab.text = [o_nameLab.text stringByReplacingCharactersInRange:NSMakeRange(o_nameLab.text.length - 1, 1) withString:@"*"];
        }
        
        if (o_add_lab.text.length > 11 ) {
            o_add_lab.text = [o_add_lab.text stringByReplacingCharactersInRange:NSMakeRange(o_add_lab.text.length - 5, 5) withString:@"*******"];
        }
        
        if (o_phoneLab.text.length > 12) {
            NSArray *strs = [o_phoneLab.text componentsSeparatedByString:@":"];
            if (strs.count>=2) {
                NSString *phone = strs[1];
                phone = [phone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
                o_phoneLab.text = [NSString stringWithFormat:@"联系电话:%@",phone];
            }
        }
        if (codeLab.text.length > 5) {
            codeLab.text = [codeLab.text stringByReplacingCharactersInRange:NSMakeRange(codeLab.text.length/2-2, 5) withString:@"*****"];
        }
    }

    isDamaflag = !isDamaflag;
    damaBtn.selected = !damaBtn.selected;
}

@end
